import Cookies from 'js-cookie'

const TokenKey = 'news-board-login-token'
const InfoKey = 'news-board-login-info'

export function getToken() {
  return Cookies.get(TokenKey)
}

export function setToken(token) {
  return Cookies.set(TokenKey, token)
}

export function removeToken() {
  return Cookies.remove(TokenKey)
}

export function getCacheInfo() {
  const info = Cookies.get(InfoKey) || '{}'
  try {
    return JSON.parse(info)
  } catch(e) {
    console.log('getInfo', e)
    return {}
  }
}

export function setCacheInfo(obj) {
  const infoStr = typeof obj === 'string' ? obj : JSON.stringify(obj)
  Cookies.set(InfoKey, infoStr)
}

export function removeCacheInfo() {
  return Cookies.remove(InfoKey)
}
